/* ==========================================
 * JGraphT : a free Java graph-theory library
 * ==========================================
 *
 * Project Info:  http://jgrapht.sourceforge.net/
 * Project Creator:  Barak Naveh (http://sourceforge.net/users/barak_naveh)
 *
 * (C) Copyright 2003-2008, by Barak Naveh and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/* --------------------
 * CompleteGraphDemo.java
 * --------------------
 * (C) Copyright 2003-2008, by Tim Shearouse and Contributors.
 *
 * Original Author:  Tim Shearouse
 * Contributor(s):   -
 *
 * $Id: CompleteGraphDemo.java 1258 2013-04-18 20:27:18Z freecode4susant $
 *
 * Changes
 * -------
 *
 */

package org.jgrapht.demo;

import org.jgrapht.Graph;
import org.jgrapht.VertexFactory;
import org.jgrapht.generate.CompleteGraphGenerator;
import org.jgrapht.graph.ClassBasedVertexFactory;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import org.jgrapht.traverse.DepthFirstIterator;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @version version
 * @since since
 * @history Created on 04/18/2013
 * @author Susanta Tewari
 */
public final class CompleteGraphDemo {


    // Number of vertices
    static int size = 10;
    static Graph<Object, DefaultEdge> completeGraph;

    private CompleteGraphDemo() {}

    public static void main(final String[] args) {


        // Create the graph object; it is null at this point
        completeGraph = new SimpleGraph<>(DefaultEdge.class);


        // Create the CompleteGraphGenerator object
        final CompleteGraphGenerator<Object, DefaultEdge> completeGenerator =
            new CompleteGraphGenerator<>(size);


        // Create the VertexFactory so the generator can create vertices
        final VertexFactory<Object> vFactory = new ClassBasedVertexFactory<>(Object.class);


        // Use the CompleteGraphGenerator object to make completeGraph a
        // complete graph with [size] number of vertices
        completeGenerator.generateGraph(completeGraph, vFactory, null);


        // Now, replace all the vertices with sequential numbers so we can ID
        // them
        final Set<Object> vertices = new HashSet<>();

        vertices.addAll(completeGraph.vertexSet());

        Integer counter = 0;

        for (final Object vertex : vertices) {
            replaceVertex(vertex, counter++);
        }


        // Print out the graph to be sure it's really complete
        final Iterator<Object> iter = new DepthFirstIterator<>(completeGraph);

        while (iter.hasNext()) {

            final Object vertex = iter.next();

            System.out.println("Vertex " + vertex.toString() + " is connected to: "
                               + completeGraph.edgesOf(vertex).toString());
        }
    }

    public static boolean replaceVertex(final Object oldVertex, final Object newVertex) {

        if ((oldVertex == null) || (newVertex == null)) {
            return false;
        }

        final Set<DefaultEdge> relatedEdges = completeGraph.edgesOf(oldVertex);

        completeGraph.addVertex(newVertex);

        for (final DefaultEdge e : relatedEdges) {

            final Object sourceVertex = completeGraph.getEdgeSource(e);
            final Object targetVertex = completeGraph.getEdgeTarget(e);

            if (sourceVertex.equals(oldVertex) && targetVertex.equals(oldVertex)) {
                completeGraph.addEdge(newVertex, newVertex);
            } else {

                if (sourceVertex.equals(oldVertex)) {
                    completeGraph.addEdge(newVertex, targetVertex);
                } else {
                    completeGraph.addEdge(sourceVertex, newVertex);
                }
            }
        }

        completeGraph.removeVertex(oldVertex);

        return true;
    }
}

// End CompleteGraphDemo.java



